package com.briup.jz.service.impl;

import java.util.Date;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.briup.jz.bean.AccountCustomer;
import com.briup.jz.bean.AccountCustomerExample;
import com.briup.jz.bean.AccountEmployeeExample;
import com.briup.jz.bean.extend.AccountApplyExtend;
import com.briup.jz.bean.extend.AccountCustomerExtend;
import com.briup.jz.dao.AccountCustomerMapper;
import com.briup.jz.dao.extend.AccountCustomerExtendMapper;
import com.briup.jz.service.AccountCustomerService;

import com.briup.jz.utils.CustomerException;
@Service
public class AccountCustomerServiceImpl implements AccountCustomerService{

	@Autowired
	private AccountCustomerMapper accountcustomerMapper;

	@Autowired
	private AccountCustomerExtendMapper accountcustomerExtendMapper;

	@Override
	public void saveOrUpdate(AccountCustomer accountcustomer) throws CustomerException {
		// TODO Auto-generated method stub
		if(accountcustomer.getId()!=null) {
			accountcustomerMapper.updateByPrimaryKey(accountcustomer);
		}else {
			// 判断是否有同名，如果有抛出异常
//			AccountCustomerExample example = new AccountCustomerExample();
//			example.createCriteria().andIdEqualTo(AccountCustomer.getId());
//			List<AccountCustomer> list = accountcustomerMapper.selectByExample(example);
//			if(list.size()>0) {
//				throw new CustomerException("同名已存在");
			accountcustomer.setStatus("正常");
			accountcustomer.setTransferTime((new Date().getTime()));
			accountcustomerMapper.insert(accountcustomer);
			}
	}
	

	@Override
	public List<AccountCustomer> query(Long id,String status,String type,Long userId) {
	
		/*模板*/
		AccountCustomerExample example=new AccountCustomerExample();
		/*条件查询*/
		AccountCustomerExample.Criteria criteria = example.createCriteria();
		if(id!=null) {
			criteria.andIdEqualTo((long)id);
		}
		if(status!=null) {
			criteria.andStatusEqualTo(status);
		}
		if(type!=null) {
			criteria.andTypeEqualTo(type);
		}
		if (userId!=null) {
			criteria.andUserIdEqualTo(userId);
		}
		
		List<AccountCustomer> list = accountcustomerMapper.selectByExample(example);
	/*	for(AccountCustomer a:list) {
			System.out.println(a);
		}*/
//		System.out.println("zzzzzz");
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		// TODO Auto-generated method stub
		AccountCustomer address=accountcustomerMapper.selectByPrimaryKey(id);
		if(address==null) {
			throw new CustomerException("要删除不存在");
		}
		accountcustomerMapper.deleteByPrimaryKey(id);
	}
//  级联多符合查询
	@Override
	public List<AccountCustomerExtend> queryCascade(String status, String type, Long userId) {
		return accountcustomerExtendMapper.select(status, type, userId);

	}
	}


